#include <pthread.h>
#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <string.h>
#include "../../Outils/config.h"
#include "../../Outils/simulation.h"
#include "../conditionnement.h"
#include "../impression.h"
#include "../stockage.h"
#include "../destockage.h"
#include "../palette.h"

/* Threads */
static void *Lecture (void *);
static void *Archivage (void *args); 
pthread_mutex_t wait_mutex;

#define FAUX 0
#define ZERO 0

int main()
{
	printf("TEST\n");
	LancerSimu();
	//Boites aux lettres 
	mqd_t lecture_conditionnement, conditionnement_impression, conditionnement_archivage;	
	mqd_t impression_palette; 
	 pthread_t lectureThread, paletteThread, archivageThread, mainThread, anotherThread;

	/* ----- mutex */
	pthread_mutex_t mutex_SHM_Stock;
	MutexSHMStock=&mutex_SHM_Stock;
	pthread_mutex_init(MutexSHMStock,NULL);
	
	pthread_mutex_t mutex_SHM_OF;
	MutexSHMOF=&mutex_SHM_OF;
	pthread_mutex_init(MutexSHMOF,NULL);
	
	struct mq_attr lesAttr={0,1,sizeof(MSG_demarrage),0};

	struct mq_attr lesAttr2={0,2,sizeof(MSG_carton),0};

	struct mq_attr lesAttr3={0,10,sizeof(MSG_versArchivage),0};

	struct mq_attr lesAttr4={0,10,sizeof(MSG_carton),0};

	lecture_conditionnement = mq_open(BAL_demarrage,O_CREAT|O_RDWR,0777,&lesAttr);
  	conditionnement_impression = mq_open(BAL_versImpression,O_CREAT|O_RDWR,0777,&lesAttr2);
	conditionnement_archivage = mq_open(BAL_versArchivage,O_CREAT|O_RDWR,0777,&lesAttr3);   
	impression_palette = mq_open(BAL_versPalette,O_CREAT|O_RDWR,0777,&lesAttr4);	
	
	 printf ("Creating threads .. \n");
	pthread_create (&lectureThread,NULL,Lecture,NULL);
	 pthread_create (&mainThread,NULL,Conditionnement,NULL);
	 pthread_create (&anotherThread, NULL,Impression, NULL);
	pthread_create (&archivageThread, NULL,Archivage, NULL);
	pthread_create (&paletteThread, NULL,Palette, NULL);
	 pthread_mutex_init (&wait_mutex, NULL);
	 
	 pthread_join (mainThread,    NULL);
	 pthread_join (anotherThread, NULL);

	pthread_cancel(lectureThread);

	mq_close(lecture_conditionnement);
	mq_unlink(BAL_demarrage);

	mq_close(conditionnement_impression);
	mq_unlink(BAL_versImpression);

	mq_close(impression_palette);
	mq_unlink(BAL_versImpression);
	
	/* ----- mutex */
	pthread_mutex_destroy(MutexSHMStock);
	pthread_mutex_destroy(MutexSHMOF);
	ArreterSimu();

	return 0; 
}

static void *Lecture (void *args)
{
	printf("[Lecture]\n");
	//Priorite des messages envoyes 
	unsigned int priorite_message = 0;
	mqd_t lecture_conditionnement;
	MSG_demarrage msg_demarrage; 
	msg_demarrage.msg = 0; 
	lecture_conditionnement = mq_open(BAL_demarrage, O_WRONLY);
	mq_send (lecture_conditionnement,(char*)&msg_demarrage,sizeof(MSG_demarrage),priorite_message);
	mq_close(lecture_conditionnement); 
}

static void *Archivage (void *args)
{
	printf("[Archivage]\n");
	//Priorite des messages envoyes 
	mqd_t erreur_conditionnement;
	MSG_versArchivage msg_versArchivage; 
for(;;)
{
	erreur_conditionnement = mq_open(BAL_versArchivage, O_RDONLY);
	int bytes_read = mq_receive(erreur_conditionnement,(char*)&msg_versArchivage,sizeof(MSG_versArchivage),NULL);
	if (bytes_read == -1)
	{
		perror("[Archivage] Lecture Pas OK \n"); 
		return 0; 
	}
}
	mq_close(erreur_conditionnement); 
}

